<?php
/*****************以下代码用于新模板*********/
//以下代码片段是创建新模板中选择  模板类型   动态触发 选择创建的模板 出现的下拉框
if($_GET['isJax'] == 1 && !empty($_GET['TType']))
{
	 include("inc/new_template_fields_array.php");
	 echo '<option value="">选择创建的模板</option>';
	 foreach($TypeNameArray[$_GET['TType']] AS $key=>$value)
	 {
		 echo '<option value="'.$value.'">'.$value.'</option>';
	 }
	 exit;
}
/*****************以上代码用于新模板*********/
include 'inc/head.php';
include 'wondershare/configs/site_config.php';

$web_type = $_GET['web_type'];

if($_GET['type'] == 'all') //更新全部模板
{
	for ($i = 11 ; $i <= 300; $i++) {
	
		if ($db->num_rows($db->query("SHOW TABLES LIKE 'Table_{$i}'")) == 1) {
			
			$sql = "SELECT * FROM Template  WHERE TID = '{$i}'";
			$rows = $GLOBALS['db']->getOneRs($sql);
			if(!empty($rows)) {
				
				foreach ($rows AS $key => $value) {
					$rows[$key] = addslashes($value);
				}
				
				$Template = new Template($rows);
				$req = $Template->update('all');
			}
		} 
	}
	
	alert("sucess","location","templateList.php?web_type={$web_type}");
	exit;
}

$TID = $_GET['TID'];
$Template = new Template(array("TID"=>0));
$Templates = $Template->allchildren();

if(is_numeric($TID))
{
	$Template->getObjById($TID, "TID", "Template");
	if($_GET['action'] == 'showCreateTable')
	{
		echo $Template->getCreateTable();
		exit;
	}
}

if($_POST['doPost'] == "true")
{
	$Template = new Template($_POST);
	if(is_numeric($TID)){
		$req = $Template->update();

	}
	else{
		$req = $Template->add();
		 
	}
	if($req)
	{
		new ActionLog(array("LogType"=>"template_sucess","Action"=>"$req\t{$currAdmin->AID}"));
		alert("sucess","location","templateList.php?web_type={$web_type}");
	}
	else
	{
		new ActionLog(array("LogType"=>"template_err","Action"=>"$req\t{$currAdmin->AID}"));
		alert("err","location","templateList.php?web_type={$web_type}");
	}
	exit;
}

/******2010.05.05预览功能修改***************/
$typeVal[] = 0;
$typeKey[] = '根';

for($i = 0; $i < count($Templates); $i++)
{
	if($Templates[$i]->PID == 0)
	{
		$attr = "|-";
		$showInList = true;
	}
	else
	{
		if($Templates[$i]->PID == $Templates[$i-1]->TID)
			$attr = "　".$attr;
	}
	if($Templates[$i]->TID == $Template->TID)
	{
		$showInList = false;
	}
	if($showInList)
	{
		$typeVal[] = $Templates[$i]->TID;
		$typeKey[] = $attr.$Templates[$i]->TName;
	}
}
/******end***************/
?>

<form method="post"   id ="from_submit" name ="from_submit"   onsubmit="return confirm_submit()">
<input type="hidden" value="true" name="doPost">

<?php if ($Template->TID) { ?> 
	<input type="hidden" value="<?php echo $Template->TID; ?>" name="TID" id="TID">
	<input type="hidden" value="<?php echo $Template->TypeName; ?>" name="TypeName" id="TypeName">
<?php } ?> 
<table width="90%" cellpadding="1" cellspacing="1" class="list-div">
	<tr>
		<th colspan="2" style="text-align:left"> <?php $FID > 0 ? "修改":"新建"; ?>模板</th>   
    </tr>
	<tr style="font-weight:bold">
		<td width="100">项目</td>
		<td>值 <?php if($TID){ echo "<a href=\"fieldList.php?TID=$TID\">模板域列表</a>"; } ?></td>
	</tr>
	<tr>
		<td width="100">名称</td>
		<td><input type="text" name="TName" value="<?php echo $Template->TName; ?>" class="input-text" /></td>
	</tr>
	<tr>
		<td width="100">默认路径</td>
		<td><input type="text" name="UrlRule" value="<?php echo $Template->UrlRule; ?>" class="input-text" /></td>
	</tr>
	
	
	<tr> 
		<td width="100">模板类型</td>
		<td><select name="TType" style="width:250px;" id="TType" <?php if(!$Template->TID){ ?>  onchange="showTemplate();" <?php }?>>
		<option value="">请选择模板类型</option>
		<option value="Products" <?php if( $Template->TType == 'Products'){ ?> selected="selected" <?php  }?> >Products</option>
		<option value="Article" <?php if( $Template->TType == 'Article'){ ?> selected="selected" <?php }?>  >Article</option>
		<option value="Support" <?php if( $Template->TType == 'Support'){ ?> selected="selected" <?php }?>   >Support</option>
		<option value="Buy" <?php if( $Template->TType == 'Buy'){  ?> selected="selected" <?php }?>   >Buy</option>
		<option value="Company" <?php if( $Template->TType == 'Company'){ ?> selected="selected" <?php }?>  >Company</option>
		<option value="Other" <?php if( $Template->TType == 'Other'){ ?> selected="selected" <?php }?>  >Other</option>
		</select></td>
	</tr>
	<?php if(!$Template->TID){ ?> 
    <tr style="display:none;" id="TypeNameTr"> 
		<td width="100">选择创建的模板</td>
		<td><select name="TypeName" style="width:250px;" id="TypeName" >
		</select><span style="color:#FF0000; margin-left:30px;">切记！请认真选择创建的模板，创建后不能更改.</span></td>
		
	</tr>
    <?php }else{?>
    <?php if($Template->TypeName !=''){?>
     <tr id="TypeNameTr"> 
		<td width="100">你选择的模板</td>
		<td><span style="color:#FF0000"><?php echo $Template->TypeName;?></span></td>		
	</tr>
    <?php }}?>
    
    <tr> 
		<td>所属站点</td>
		<td>
			<select name="web_type">
			<option value="0">选择所属站点</option>
			<?php foreach( $site_name as $k=>$v ){?>
			<option value="<?php echo $k;?>" <?php if($Template->web_type==$k){?>selected="selected"<?php }?>><?php echo $v;?></option>
			<?php }?>
			<option value="0" <?php if($Template->web_type==0){?>selected="selected"<?php }?>>其它</option>
			</select>
			</td>		
	</tr>
    
    
	<tr style="display:none">
		<td width="100">表名</td>
		<td><input type="text" name="DTName" value="<?php echo $Template->DTName; ?>"></td>
	</tr>
	<tr>
		<td width="100" valign="top">模板内容</td>
	  <td><textarea name="Style" cols="100" rows="20"><?php echo htmlspecialchars($Template->Style); ?></textarea></td>
	</tr>
	<tr>
		<td width="100" valign="top">配置</td>
	  <td><textarea name="Config" cols="100" rows="3"><?php echo htmlspecialchars($Template->Config); ?></textarea></td>
	</tr>
	<tr>
		<td width="100" valign="top">脚本<div>(请只写javascript脚本代码，不用&lt;script&gt;标签，可以使用jquery库函数)</div></td>
	  <td><textarea name="Script" cols="100" rows="20"><?php echo htmlspecialchars($Template->Script); ?></textarea></td>
	</tr>
	<!--2010.05.05预览功能修改-->
	<tr>
		<td width="100">父模块</td>
		
	  <td><select name="PID">
		<?php
		for($i = 0; $i < count($typeVal); $i++)
		{
			echo "<option value=\"{$typeVal[$i]}\"";
			if($Template->PID == $typeVal[$i])
				echo " selected=\"selected\"";
			echo ">{$typeKey[$i]}</option>\n";
		}
		?>
		</select></td>
	</tr>
	<!--end-->
	<tr>
		<td width="100"></td>
		<td><input type="submit" name="sub" value="提交"> <input type="button" onclick="history.go(-1)" value="返回" /></td>
	</tr>
</table>

</form>
<script>
function confirm_submit()
{
	//修改确认
	if ($("#TID").val() != 0) {
		if (confirm("是否确认修改?"))  {  
			return true;
		
		}  else  { 
			return false;
		};
	} else {
		return true;
	}
}
function  showTemplate()
{
	 var  TType = $("#TType").val();
	 if(TType != '')
	 {
		 $("#TypeName").html('');
		 $("#TypeNameTr").show();
		 $.get("template.php", { TType: TType,isJax:1 },
		 function(data){
			
		  $("#TypeName").append(data);
		}); 
	 }else
	 {
		 $("#TypeNameTr").hide();
	 }
	   
}

</script>
<?php
include("inc/foot.php");
?>
